/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lfk.justwetools.Utils;

import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class ValidatorsUtilsTest {

    @Test
    public void isAlphanumeric() {
        assertTrue(ValidatorsUtils.isAlphanumeric("abc123"));
        assertTrue(ValidatorsUtils.isAlphanumeric("123abc"));
        assertTrue(ValidatorsUtils.isAlphanumeric("1a2b3c"));
        assertTrue(ValidatorsUtils.isAlphanumeric("123"));
        assertFalse(ValidatorsUtils.isAlphanumeric("你好"));
        assertFalse(ValidatorsUtils.isAlphanumeric("123."));
        assertFalse(ValidatorsUtils.isAlphanumeric("abc."));
        assertFalse(ValidatorsUtils.isAlphanumeric(""));
    }

    @Test
    public void isBlank() {
        assertTrue(ValidatorsUtils.isBlank(null));
        assertTrue(ValidatorsUtils.isBlank(""));
        assertTrue(ValidatorsUtils.isBlank(" "));
        assertFalse(ValidatorsUtils.isBlank("bob"));
        assertFalse(ValidatorsUtils.isBlank(" bob "));
    }

    @Test
    public void isChinaMobile() {
        //"1(3[4-9]|4[7]|5[012789]|8[278])\\d{8}"
        assertFalse(ValidatorsUtils.isChinaMobile(null));
        assertFalse(ValidatorsUtils.isChinaMobile(""));
        assertFalse(ValidatorsUtils.isChinaMobile("13312345678"));
        assertTrue(ValidatorsUtils.isChinaMobile("13412345678"));
        assertTrue(ValidatorsUtils.isChinaMobile("13512345678"));
        assertTrue(ValidatorsUtils.isChinaMobile("13612345678"));
        assertTrue(ValidatorsUtils.isChinaMobile("13712345678"));
        assertTrue(ValidatorsUtils.isChinaMobile("13812345678"));
        assertTrue(ValidatorsUtils.isChinaMobile("13912345678"));
        assertFalse(ValidatorsUtils.isChinaMobile("139123456780"));
        assertTrue(ValidatorsUtils.isChinaMobile("14712345678"));
        assertFalse(ValidatorsUtils.isChinaMobile("14812345678"));
        assertTrue(ValidatorsUtils.isChinaMobile("15012345678"));
        assertTrue(ValidatorsUtils.isChinaMobile("15112345678"));
        assertTrue(ValidatorsUtils.isChinaMobile("15212345678"));
        assertFalse(ValidatorsUtils.isChinaMobile("15312345678"));
        assertTrue(ValidatorsUtils.isChinaMobile("15712345678"));
        assertTrue(ValidatorsUtils.isChinaMobile("15812345678"));
        assertTrue(ValidatorsUtils.isChinaMobile("15912345678"));
        assertTrue(ValidatorsUtils.isChinaMobile("18212345678"));
        assertTrue(ValidatorsUtils.isChinaMobile("18712345678"));
        assertTrue(ValidatorsUtils.isChinaMobile("18812345678"));
        assertFalse(ValidatorsUtils.isChinaMobile("18912345678"));
    }

    @Test
    public void isChinaUnicom() {
        //"1(3[0-2]|5[56]|8[56])\\d{8}"
        assertFalse(ValidatorsUtils.isChinaUnicom(null));
        assertFalse(ValidatorsUtils.isChinaUnicom(""));
        assertTrue(ValidatorsUtils.isChinaUnicom("13012345678"));
        assertTrue(ValidatorsUtils.isChinaUnicom("15512345678"));
        assertTrue(ValidatorsUtils.isChinaUnicom("18512345678"));
        assertFalse(ValidatorsUtils.isChinaUnicom("13312345678"));
        assertFalse(ValidatorsUtils.isChinaUnicom("15812345678"));
        assertFalse(ValidatorsUtils.isChinaUnicom("18712345678"));
    }

    @Test
    public void isChinaTelecom() {
        //"(?!00|015|013)(0\\d{9,11})|(1(33|53|80|89)\\d{8})"
        assertFalse(ValidatorsUtils.isChinaTelecom(null));
        assertFalse(ValidatorsUtils.isChinaTelecom(""));
        assertTrue(ValidatorsUtils.isChinaTelecom("13312345678"));
        assertTrue(ValidatorsUtils.isChinaTelecom("15312345678"));
        assertTrue(ValidatorsUtils.isChinaTelecom("18012345678"));
        assertTrue(ValidatorsUtils.isChinaTelecom("18912345678"));
        assertFalse(ValidatorsUtils.isChinaTelecom("13412345678"));
        assertFalse(ValidatorsUtils.isChinaTelecom("15412345678"));
        assertFalse(ValidatorsUtils.isChinaTelecom("18112345678"));
        assertFalse(ValidatorsUtils.isChinaTelecom("18812345678"));
    }

    @Test
    public void isEmail() {
        assertTrue(ValidatorsUtils.isEmail("123@163.com"));
        assertTrue(ValidatorsUtils.isEmail("123@123.cn"));
        assertTrue(ValidatorsUtils.isEmail("123@abc.xyz"));
        assertFalse(ValidatorsUtils.isEmail("123@111"));
        assertFalse(ValidatorsUtils.isEmail("123@"));
        assertFalse(ValidatorsUtils.isEmail("123"));
        assertFalse(ValidatorsUtils.isEmail("@"));
    }

    @Test
    public void isEmpty() {
        Object[] array = null;
        assertTrue(ValidatorsUtils.isEmpty(array));
        array = new Integer[]{};
        assertTrue(ValidatorsUtils.isEmpty(array));
        array = new Integer[]{null};
        assertTrue(ValidatorsUtils.isEmpty(array));
        array = new Integer[]{0};
        assertFalse(ValidatorsUtils.isEmpty(array));
        array = new Integer[]{null, null};
        assertFalse(ValidatorsUtils.isEmpty(array));
    }

    @Test
    public void testIsEmpty() {
        String value = null;
        assertTrue(ValidatorsUtils.isEmpty(value));
        assertTrue(ValidatorsUtils.isEmpty(value = ""));
        assertTrue(ValidatorsUtils.isEmpty(value = " "));
        assertFalse(ValidatorsUtils.isEmpty(value = "bob"));
        assertFalse(ValidatorsUtils.isEmpty(value = " bob "));
    }

    @Test
    public void testIsEmptyCollection() {
        List<String> list = null;
        assertTrue(ValidatorsUtils.isEmpty(list));
        list = new ArrayList<>();
        assertTrue(ValidatorsUtils.isEmpty(list));
        list = new ArrayList<>();
        list.add("");
        assertFalse(ValidatorsUtils.isEmpty(list));
        list = new ArrayList<>();
        list.add("Hello");
        assertFalse(ValidatorsUtils.isEmpty(list));
    }

    @Test
    public void isIdCardNumber() {
        //"(\\d{14}|\\d{17})(\\d|x|X)"
        assertTrue(ValidatorsUtils.isIdCardNumber("123456789012341"));
        assertTrue(ValidatorsUtils.isIdCardNumber("12345678901234x"));
        assertTrue(ValidatorsUtils.isIdCardNumber("12345678901234X"));
        assertFalse(ValidatorsUtils.isIdCardNumber("12345678901234A"));
        assertFalse(ValidatorsUtils.isIdCardNumber("1234567890123411"));
        assertTrue(ValidatorsUtils.isIdCardNumber("123456789012345671"));
        assertTrue(ValidatorsUtils.isIdCardNumber("12345678901234567x"));
        assertTrue(ValidatorsUtils.isIdCardNumber("12345678901234567X"));
        assertFalse(ValidatorsUtils.isIdCardNumber("12345678901234567A"));
        assertFalse(ValidatorsUtils.isIdCardNumber("1234567890123456711"));
    }

    @Test
    public void isMobile() {
        assertFalse(ValidatorsUtils.isMobile("123456789012341"));
    }

    @Test
    public void isNumber() {
        assertTrue(ValidatorsUtils.isNumber("123456789012341"));
        assertFalse(ValidatorsUtils.isNumber(""));
        assertFalse(ValidatorsUtils.isNumber("a"));
    }

    @Test
    public void testIsNumber() {
        assertTrue(ValidatorsUtils.isNumber("3", 2, 4));
        assertTrue(ValidatorsUtils.isNumber("3456", 3455, 3457));
        assertFalse(ValidatorsUtils.isNumber("3456", 3453, 3455));
        assertFalse(ValidatorsUtils.isNumber("a", 3453, 3455));
    }

    @Test
    public void isNumeric() {
        assertTrue(ValidatorsUtils.isNumeric("3.1"));
        assertTrue(ValidatorsUtils.isNumeric("333"));
        assertFalse(ValidatorsUtils.isNumeric(".1"));
        assertFalse(ValidatorsUtils.isNumeric("."));
        assertFalse(ValidatorsUtils.isNumeric("aa"));
    }

    @Test
    public void testIsNumeric() {
        assertTrue(ValidatorsUtils.isNumeric("3.1", 2));
        assertTrue(ValidatorsUtils.isNumeric("3.12", 2));
        assertFalse(ValidatorsUtils.isNumeric("3.123", 2));
    }

    @Test
    public void isPostcode() {
        assertTrue(ValidatorsUtils.isPostcode("123456"));
        assertFalse(ValidatorsUtils.isPostcode("12345"));
        assertFalse(ValidatorsUtils.isPostcode("1234567"));
        assertFalse(ValidatorsUtils.isPostcode("aaa"));
        assertFalse(ValidatorsUtils.isPostcode(""));
    }

    @Test
    public void isString() {
        assertTrue(ValidatorsUtils.isString("123", 2, 4));
        assertTrue(ValidatorsUtils.isString("1234", 2, 5));
        assertFalse(ValidatorsUtils.isString("", 2, 5));
        assertFalse(ValidatorsUtils.isString("123456", 2, 5));
    }

    @Test
    public void isTime() {
        assertTrue(ValidatorsUtils.isTime("12:00"));
        assertTrue(ValidatorsUtils.isTime("23:59"));
        assertFalse(ValidatorsUtils.isTime("24:00"));
        assertFalse(ValidatorsUtils.isTime("24:55"));
    }

    @Test
    public void isSimpleChinese() {
        assertTrue(ValidatorsUtils.isSimpleChinese("你好"));
        assertFalse(ValidatorsUtils.isSimpleChinese("你好abc"));
    }

    @Test
    public void isRegexMatch() {
        assertTrue(ValidatorsUtils.isRegexMatch("", ".*"));
        assertTrue(ValidatorsUtils.isRegexMatch("1234", ".*"));
        assertTrue(ValidatorsUtils.isRegexMatch("adbc", ".*"));
        assertTrue(ValidatorsUtils.isRegexMatch("+-*/", ".*"));
    }
}